﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Diagnostics;
using System.Web.Security;
using KerriganDataContract;
using KerriganDataContract.Authentification;

namespace KerriganBlog
{
    public partial class Login : BasePage
    {

        private String userName;
        private String password;

        protected void Page_Load(object sender, EventArgs e)
        {

        }


        private bool AuthenticateUser(string strUser, string strPassword)
        {
            bool isAuthenticate = false;

            try
            {
                using (ServiceProxy Srv = new ServiceProxy(strUser, strPassword))
                {
                    AuthenticateUserDC UserSession = Srv.Service.CheckLoginUser(strUser, strPassword);
                    int Id = UserSession.Person.IdPerson;
                    string NickName = UserSession.Person.NickName;
                    string Password = UserSession.Person.Password;

                    if (Id != 0)
                    {
                        // Create a new ticket used for authentication
                        FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                            1,
                            strUser,
                            DateTime.Now,
                            DateTime.Now.AddMinutes(30),
                            true,
                            strUser + "," + strPassword + "," + Id ,
                            FormsAuthentication.FormsCookiePath);

                        // Encrypt the cookie using the machine key for secure transport
                        string hash = FormsAuthentication.Encrypt(ticket);
                        HttpCookie cookie = new HttpCookie("AXPXAUTHFWC", hash);

                        // Set the cookie's expiration time to the tickets expiration time
                        if (ticket.IsPersistent) cookie.Expires = ticket.Expiration;

                        // Add the cookie to the list for outgoing response
                        Response.Cookies.Add(cookie);

                        isAuthenticate = true;
                    }
                }
            }

   
            catch (Exception ex)
            {
                WebException EntryPointEx = new WebException();
                if (ex.InnerException.GetType() == EntryPointEx.GetType()) { 
                }
                    //MessageBox.Show("Serveur indisponible ou non accessible.");
                //else
                    //MessageBox.Show(ex.Message.ToString());

                //Debug.WriteLine(ex.Message.ToString());
            }

            return isAuthenticate;
        }

        protected void LoginButton_Click(object sender, EventArgs e)
        {
            if (AuthenticateUser(((TextBox)FindControl("UserName")).Text, ((TextBox)FindControl("Password")).Text))
                Response.Redirect("~/ManageArticleHome.aspx");
        }



    }
}